
package uk.ac.cam.cl.oscar.Oscar_Mobile_Server;

import java.net.Socket;
import java.awt.image.*;
import java.io.*;
import java.util.*;

import uk.ac.cam.cl.oscar.*;
import uk.ac.cam.cl.oscar.sift.*;

        
public class ImageProcessor implements Runnable{
    byte[] imageByteArray;
    BufferedImage image;
    Socket socket;
    ImageRecord[] matches;
    DataInputStream dis;
    DataOutputStream dos;
    
    public ImageProcessor(Socket s){
        this.socket = s;
    }
    
    public void run(){
        try{
            //receive image
            dis = new DataInputStream(socket.getInputStream());
            dos = new DataOutputStream(socket.getOutputStream());
            
            image = javax.imageio.ImageIO.read(dis);
            System.out.println(image.getWidth() + " " + image.getHeight());
            //process image
            System.out.printf("Image received%n");
            ImageFinder imageFinder = new ImageFinder();
            
            if(Demonstration.DemoAppInst != null){
                Demonstration.DemoAppInst.processNewRequest(this);
                Demonstration.DemoAppInst.displayNewInitImage(this.toString(), image);
            }
            
            Vector<ImageRecord> vm = ImageFinder.getMatches(image);
            matches = new ImageRecord[vm.size()];
            ListIterator li = vm.listIterator();
            int i = 0;
            while(li.hasNext()){
                matches[i] = (ImageRecord)li.next();
                i++;
            }
            System.out.printf("Miracle%n");
            //return results
            SendResponse sendResponse = new SendResponse();
            sendResponse.send(matches, dos);
        }
        catch(IOException e){
            
        }
        
        
    }
}
